# -*- coding: utf-8 -*-
"""
Word文档转Markdown命令行工具
"""

import sys
from pathlib import Path
import time

from docx_converter import DocxConverter


def convert_file(filename: str) -> bool:
    """
    转换单个docx文件为markdown
    
    Args:
        filename: 文件名
        
    Returns:
        bool: 是否成功
    """
    converter = DocxConverter()
    
    input_dir = Path("input_docxs")
    input_dir.mkdir(parents=True, exist_ok=True)
    
    input_file = input_dir / filename
    
    if not input_file.exists():
        print(f"❌ 文件不存在: {input_file}")
        return False
    
    if input_file.suffix.lower() not in ['.docx', '.doc']:
        print(f"❌ 不支持的文件格式: {input_file.suffix}")
        return False
    
    try:
        print(f"📝 开始转换: {filename}")
        start_time = time.time()
        
        md_file, images = converter.convert(str(input_file))
        
        elapsed = time.time() - start_time
        print(f"✅ 转换成功!")
        print(f"   耗时: {elapsed:.2f}秒")
        print(f"   输出: {md_file}")
        print(f"   图片: {len(images)}张")
        return True
        
    except Exception as e:
        print(f"❌ 转换失败: {e}")
        return False


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("用法: python convert.py <文件名.docx>")
        sys.exit(1)
    
    filename = sys.argv[1]
    success = convert_file(filename)
    sys.exit(0 if success else 1)
